1. Field of the Present Invention
The present invention relates to the field of computer graphics and more particularly to a method and system for managing a complex graphics state on an adapter supporting multiple threads running in a windowing-type operating system environment.
2. History of Related Art
Graphics adapters have become a standard I/O peripheral device for microprocessor based computing systems. Some graphics adapters incorporate the ability to rastorize graphic primitives using state information that specify graphics attributes, such as pixel depth, that are set by graphics commands. Theses attributes are referred to as the adapter context. For these graphic adapters, software must insure that when a thread sends commands to the adapter, the commands are interpreted using the correct adapter context. In addition, software must insure that only one thread accesses the adapter at any given time.
Many widely distributed operating systems enable multi-tasking operation in which two or more application programs are executing simultaneously. Typically, each application program may spawn one or more threads where each thread is dedicated to a specific task. In addition, most multi-tasking operating systems provide a protection mechanism to prevent application programmers from accessing or otherwise modifying code that is essential to the operating system. In a Windows(copyright) environment, for example, a protection mechanism is provided by assigning application code to one of four hierarchical xe2x80x9crings,xe2x80x9d rings 0 through 3, where ring 0 code is the most highly protected code (accessible only to other ring 0 code) and ring 3 code is the least protected code (accessible to code of all rings). In a similar manner, Unix(copyright) systems utilize a xe2x80x9ckernelxe2x80x9d mode and a xe2x80x9cuserxe2x80x9d mode. While the present disclosure is described with frequent references to the ring protection mechanism of the Windows(copyright) operating system, it is to be understood that the invention is not limited to a Windows(copyright) implementation and is generally applicable to any multi-tasking operating system employing some manner of protection mechanism.
In a Windows(copyright) operating environment, conventional application software is typically written as ring 3 code. At the other end of the spectrum, the core operating system routines and tasks including device drivers that are designed to configure settings on various peripheral devices are ring 0 code. If a ring 3 application program requires the use of ring 0 code, the operating system provides a mechanism by which the ring 3 code can make a call that will result in the execution of ring 0 code. Typically, the ring 3 to ring 0 transition is a time consuming and expensive transition because the operating system must initiate a sequence of complex subroutine calls. Each of these subroutine calls typically require the operating system to store the context of the calling subroutine for later retrieval. It will be appreciated by those familiar with the most common multi-tasking operating systems that it is desirable to avoid ring 3 to ring 0 transitions whenever possible. In the area of high performance computer graphics, minimizing the number of such transitions can determine the ultimate performance of the graphics application. When multiple threads that access the adapter, each with its own corresponding adapter context, are running simultaneously, the task of re-instating the appropriate adapter context that each thread must perform prior to sending adapter commands negatively affects system performance. This is especially true in memory protected operating systems in which a transition from application code to core code is initiated with each change of adapter context. Therefore, it would be highly desirable to implement a method and system that minimized operating system overhead while supporting multi-threaded graphics execution.
The problems identified above are address by a method and system for controlling access to an adapter, such as a graphics adapter. The method includes querying an adapter lock with a first thread. Thereafter, responsive to determining that the lock indicates the first thread does not have access to the adapter, a sequence to obtain access to the adapter is initiated where the sequence includes writing the adapter context corresponding to the first thread. The sequence includes a ring 3 to ring 0 transition. The method also includes, in response to determining that the lock indicates the first thread has access to the adapter, communicating to the adapter with the first thread without invoking the sequence to obtain access to the adapter. In one embodiment, querying the adapter lock includes writing a first word of the adapter lock using an atomic operation. The method may further include writing a set of command buffers with the first thread and, responsive to determining that the first has access to the adapter, transferring the commands buffers to the adapter. In one embodiment, the sequence to obtain access to the adapter includes the first thread obtaining ownership of a mutex lock prior to updating the adapter context. The sequence to obtain access to the adapter may include updating the adapter lock status to indicate the first thread having access to the adapter.
The invention further contemplates a data processing system that includes a processor, a bus bridge, and an adapter. The processor has access to a system memory via a system bus. The bus bridge is connected between the system bus and a peripheral bus. The adapter, such as a graphics adapter is connected to the peripheral bus. In one embodiment, the system memory contains a display driver suitable for modifying the context of the adapter and user code suitable for writing a set of command buffers that are executable by the adapter. The system memory preferably further includes a routine suitable for querying an adapter lock to determine if the user code has access to the adapter in response to the user code indicating that a set of command buffers is ready to be transferred to the adapter. In this manner the set of command buffers can be transferred to the adapter without invoking the display driver to modify the adapter context or placing context restoring commands at the start of each command buffer.